﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Applee.FractCommon;
using System.Windows.Forms;
using System.Drawing;

namespace Applee.SierpinskiFract
{
    public class TriangleFract : BaseFract
    {
        //protected override void CreateFract(int iterations)
        //{
        //    //mLength = mViewSize.Width - 10;
        //    CalcLength();
        //    CreateFract(new PointF(0, 0), mColorGen, mLength, iterations);
        //}

        protected override void Initialize()
        {
            float lcbAngle = (float)(30.0F * Math.PI / 180);
            SizeF shapeSize = new SizeF((float)(Math.Tan(lcbAngle) + 1.0 / Math.Cos(lcbAngle)), 1.0F);
            float flength = 0;

            if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
            {
                //width
                //2Rcos18=width
                //R=L/(sin36x2)
                //L=sin36/cos18xwidth
                mLength = (float)(Math.Tan(lcbAngle) + 1.0 / Math.Cos(lcbAngle)) * flength;


            }
            else
            {
                //Height
                //Rcos36+R = height
                //R=L/(sin36x2)
                //L=heightx(sin36x2)/(cos36+1)
                mLength = 1.0F * flength;
            }
        }

        protected override void CreateFract(PointF point, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            if (fLength < 1.0F)
                return;

            EqualTriangle et = new EqualTriangle();
            et.MakeShape(point, fLength);
            et.Color = color.GetColor(iterations);

            mFractObjects.Add(et);

            EqualTriangle etTemp = new EqualTriangle();
            etTemp.MakeShape(point, fLength * 0.5F);

            CreateFract(etTemp.Points[0], color, fLength * 0.5F, iterations - 1);
            CreateFract(etTemp.Points[1], color, fLength * 0.5F, iterations - 1);
            CreateFract(etTemp.Points[2], color, fLength * 0.5F, iterations - 1);
        }
    }
}
